﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using it_security_project.App_Code.DataAccessLayer;
using System.Threading;

namespace it_security_project.App_Code.BusinessLogicLayer
{
    public class Service
    {
        private MemberDAL _memberDAL;
        private ThreadDAL _threadDAL;
        private LogDAL _logDAL;

        MemberDAL MemberDAL
        {
            get
            {
                return this._memberDAL ?? (this._memberDAL = new MemberDAL()); 
            }
        }

        ThreadDAL ThreadDAL
        {
            get
            {
                return this._threadDAL ?? (this._threadDAL = new ThreadDAL());
            }
        }

        LogDAL LogDAL
        {
            get
            {
                return this._logDAL ?? (this._logDAL = new LogDAL());
            }
        }
        
        public void RegisterUser(Member member)
        {
            if (member.IsValid)
            {
               MemberDAL.RegisterMember(member);
            }
            else
            {
                ApplicationException ex = new ApplicationException(member.Error);
                ex.Data.Add("Member", member);
                throw ex;
            }
        }

        public void AddLog(int userID, string action)
        {
            this.LogDAL.AddLog(userID, action);
        }

        public void SelectUsers()
        {
        }

        public List<Thread> GetThreadsByUsername(string username)
        {
            return this.ThreadDAL.GetThreadsByUsername(username);
        }

        public void DeleteThread(int ID)
        {
            this.ThreadDAL.DeleteThread(ID);
        }

        public List<Comment> GetCommentsOnThreadId(string thread)
        {
            try
            {
                return this.ThreadDAL.GetCommentsOnThreadId(int.Parse(thread));
            }
            catch
            {
                //throw new Exception("Feeeel!!");
            }
            return null;
        }

        public List<Thread> GetThreads()
        {
            return this.ThreadDAL.GetThreads();
        }

        public Thread GetThreadById(int id)
        {
            return this.ThreadDAL.GetThreadById(id);
        }

        public Member GetUserPasswordOnUser(string username)
        {
            Member member = MemberDAL.GetMemberByUsername(username);
            if (member.IsValid)
            {
                return member;
            }
            else
            {
                ApplicationException ex = new ApplicationException(member.Error);
                ex.Data.Add("Member", member);
                throw ex;
            }
        }

        public Member GetUserUsernameOnUser(string username)
        {
            return MemberDAL.GetUsernameIDOnUser(username);
        }

        public void AddThread(Thread thread)
        {
            this.ThreadDAL.AddThread(thread);
        }

        public void AddComment(int userID, string comment, int threadID)
        {
            this.ThreadDAL.AddComment(userID, comment, threadID);
        }

        internal void addBan(string UserIPAddress, long ticks)
        {
            MemberDAL.Ban(UserIPAddress, ticks);
        }

        internal bool IsBanned(string IP, string time)
        {
            return MemberDAL.IsBanned(IP, time);
        }

        internal void ChangePassword(string username, string password)
        {
            MemberDAL.ChangePassword(username, password);
        }

        internal void ChangeEmail(string username, string email)
        {
            MemberDAL.ChangeEmail(username, email);
        }
    }
}